import settings
from dinggroup import Robot


class ReceiveMessage:
    '''
    机器人接收到的消息类
    eg：
    {
        "conversationId": "cidXh/HjytF0QW+K9j16/Ddhw==",
        "sceneGroupCode": "noPrivateChat",
        "atUsers": [
            {"dingtalkId": "$:LWCP_v1:$ZzJ24WgSIrVMEKeW+jRxCc9r3tzGgby6"}
        ],
        "chatbotUserId": "$:LWCP_v1:$ZzJ24WgSIrVMEKeW+jRxCc9r3tzGgby6",
        "msgId": "msgNjgRhBx7hpmesWEOrVBwXw==",
        "senderNick": "哎呀",
        "isAdmin": False,
        "sessionWebhookExpiredTime": 1660758578704,
        "createAt": 1660753178528,
        "conversationType": "2",
        "senderId": "$:LWCP_v1:$HdvaVGcJo1v+fY3EWi08Em220jEG+g",
        "conversationTitle": "xxx群",
        "isInAtList": True,
        "sessionWebhook": "https://oapi.dingtalk.com/robot/sendBySession?session=xxx",
        "text": {"content": " 这是一条消息\n"},
        "robotCode": "normal",
        "msgtype": "text"
    }
    '''

    def __init__(self, message: dict):
        for key, value in message.items():
            setattr(ReceiveMessage, key, value)


class MessageHandler:
    def __init__(self, msg: object):
        self.msg = msg

    def reply(self):
        keyword_value = settings.KEYWORD_REPLY.get(self.msg.text['content'].strip())
        print(keyword_value, self.msg.text['content'].strip())
        if keyword_value:
            rb = Robot()
            func = getattr(rb, keyword_value['func'])
            res = func(**keyword_value['content'], url=self.msg.sessionWebhook)
            print(res)
            return res
